package com.cynoware.busappmas.contentprovider;


import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;

import com.cynoware.busappmas.model.GPSLocalInfo;

public class DbGPSInfo{
	private ContentResolver mContentResolver;
	
	public DbGPSInfo(ContentResolver mContentResolver){
		this.mContentResolver = mContentResolver;
	}
	
	public void insertGPSLocalInfo(GPSLocalInfo info){
		String        strinfo;
		int           num = info.getInfoNum();
		ContentValues cv  = new ContentValues();

		cv.put(DbHelper.COLUME_GPRSLOCAL_STATUE, info.getStatue());
		cv.put(DbHelper.COLUME_GPRSLOCAL_NUM, num);
		if ( num > 0 ) {
			strinfo = info.getInfo0_GPSX() + "_" + info.getInfo0_GPSY() + "_" + 
					info.getInfo0_direct() + "_" + info.getInfo0_order() + "_" + 
					info.getInfo0_index();
			cv.put(DbHelper.COLUME_GPRSLOCAL_INFO0, strinfo);
			System.out.println("insertGPSLocalInfo info0 = " + strinfo);
		}
		
		if ( num > 1 ) {
			strinfo = info.getInfo1_GPSX() + "_" + info.getInfo1_GPSY() + "_" + 
					info.getInfo1_direct()  + "_" + info.getInfo1_order() + "_" + 
					info.getInfo1_index();
			cv.put(DbHelper.COLUME_GPRSLOCAL_INFO1, strinfo);
			System.out.println("insertGPSLocalInfo info1 = " + strinfo);
		}
		
		if ( num > 2 ) {
			strinfo = info.getInfo2_GPSX() + "_" + info.getInfo2_GPSY() + "_" + 
					info.getInfo2_direct()  + "_" + info.getInfo2_order() + "_" + 
					info.getInfo2_index();
			cv.put(DbHelper.COLUME_GPRSLOCAL_INFO2, strinfo);
			System.out.println("insertGPSLocalInfo info2 = " + strinfo);
		}
		
		if ( num > 3 ) {
			strinfo = info.getInfo3_GPSX() + "_" + info.getInfo3_GPSY() + "_" + 
					info.getInfo3_direct()  + "_" + info.getInfo3_order() + "_" + 
					info.getInfo3_index();
			cv.put(DbHelper.COLUME_GPRSLOCAL_INFO3, strinfo);
			System.out.println("insertGPSLocalInfo info3 = " + strinfo);
		}
		
		mContentResolver.insert(DbContentProvider.CONTENT_URI_GPSLOCAL,  cv);
	}
	
	public GPSLocalInfo getGPSLocalInfo() {
		GPSLocalInfo info   = null;
		String[] projection = {DbHelper.COLUME_GPRSLOCAL_STATUE, DbHelper.COLUME_GPRSLOCAL_NUM, DbHelper.COLUME_GPRSLOCAL_INFO0, 
							   DbHelper.COLUME_GPRSLOCAL_INFO1, DbHelper.COLUME_GPRSLOCAL_INFO2, DbHelper.COLUME_GPRSLOCAL_INFO3};
		Cursor cursor       = mContentResolver.query(DbContentProvider.CONTENT_URI_GPSLOCAL, projection, null, null, null);
		
		System.out.println("getGPSLocalInfo count = " + cursor.getCount());
		if ( cursor.getCount() != 0 ) {
			String[] arr;
			String   localinfo;
			int      num;
			
			cursor.moveToNext();
			info   = new GPSLocalInfo();
			//System.out.println("getGPSLocalInfo getString0" + cursor.getString(0));
			num    = cursor.getInt(1);
			info.setStatue(cursor.getInt(0));
			info.setInfoNum(num);
			//System.out.println("getGPSLocalInfo num = " + num);
			//System.out.println("getGPSLocalInfo statue" + cursor.getInt(0));
			if ( num > 0 ) {
				localinfo = cursor.getString(2);
				arr       = localinfo.split("\\_");
				System.out.println("getGPSLocalInfo info0" + localinfo);
				
				if ( arr[0] != null && arr[1] != null && arr[2] != null ) {
					info.setInfo0(arr[0], arr[1], Integer.valueOf(arr[2]).intValue(), 
													Integer.valueOf(arr[3]).intValue(),
													Integer.valueOf(arr[4]).intValue());
				}
			}
			
			if ( num > 1 ) {
				localinfo = cursor.getString(3);
				System.out.println("getGPSLocalInfo info1" + localinfo);
				arr       = localinfo.split("\\_");
				
				if ( arr[0] != null && arr[1] != null && arr[2] != null ) {
					info.setInfo1(arr[0], arr[1], Integer.valueOf(arr[2]).intValue(),
													Integer.valueOf(arr[3]).intValue(),
													Integer.valueOf(arr[4]).intValue());
				}
			}
			
			if ( num > 2 ) {
				localinfo = cursor.getString(4);
				System.out.println("getGPSLocalInfo info2" + localinfo);
				arr       = localinfo.split("\\_");
				
				if ( arr[0] != null && arr[1] != null && arr[2] != null ) {
					info.setInfo2(arr[0], arr[1], Integer.valueOf(arr[2]).intValue(),
													Integer.valueOf(arr[3]).intValue(),
													Integer.valueOf(arr[4]).intValue());
				}
			}
			
			if ( num > 3 ) {
				localinfo = cursor.getString(5);
				System.out.println("getGPSLocalInfo info3" + localinfo);
				arr       = localinfo.split("\\_");
				
				if ( arr[0] != null && arr[1] != null && arr[2] != null ) {
					info.setInfo3(arr[0], arr[1], Integer.valueOf(arr[2]).intValue(),
													Integer.valueOf(arr[3]).intValue(),
													Integer.valueOf(arr[4]).intValue());
				}
			}
		}
		cursor.close();
		
		return info;
	}
	
	public void deleteGPSLocalInfo(){
		mContentResolver.delete(DbContentProvider.CONTENT_URI_GPSLOCAL, null, null);
	}
}
